package com.ldw.controller;

import com.ldw.common.ErrorCode;
import com.ldw.common.UUIDImage;
import com.ldw.vo.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("upload")
public class UploadController {

    @PostMapping
    public Result upload(@RequestParam("image") MultipartFile multipartFile) throws IOException {
        if (multipartFile == null) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        //原始文件名称
        String originalFilename = multipartFile.getOriginalFilename();
        //得到文件后缀
        String stuff = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String fileName = UUIDImage.getUUID() + "." + stuff;
        String filePath = "D:\\JavaProject\\myblog\\img\\" + fileName;
        multipartFile.transferTo(new File(filePath));
        return Result.success("null");
    }
}
